package com.interview.core.date;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
	
	public static String DATE_TYPE_DAY = "day";
	public static String DATE_TYPE_HOURS = "hour";
	public static String DATE_TYPE_MIN = "minute";
	public static String DATE_TYPE_SEC = "second";
	
	public static double daysOfTwo(Date fDate, Date oDate,String type) {
		Calendar calendar1 = Calendar.getInstance();
	    Calendar calendar2 = Calendar.getInstance();
	    calendar1.setTime(fDate);
	    calendar2.setTime(oDate);
	    long milliseconds1 = calendar1.getTimeInMillis();
	    long milliseconds2 = calendar2.getTimeInMillis();
	    long diff = milliseconds2 - milliseconds1;
	    
	    if(type == null || type.equals("")){
	    	type = DATE_TYPE_DAY;
	    }
	    
	    if(type.equals(DATE_TYPE_SEC)){
	    	return new BigDecimal(diff / 1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    }else if(type.equals(DATE_TYPE_MIN)){
	    	return new BigDecimal(diff / (60 * 1000)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    }else if(type.equals(DATE_TYPE_HOURS)){
	    	return new BigDecimal(diff / (60 * 60 * 1000)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    }else if(type.equals(DATE_TYPE_DAY)){
	    	return new BigDecimal(diff / (24 * 60 * 60 * 1000)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    }else{
	    	return 0;
	    }
	}
	
	public static String formatDate(String formatString,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(formatString,Locale.US);
		return sdf.format(date);
	}
}

